# -*- Coding = utf-8 -*-
# @time: 2021/1/29 9:53
# Author: YKL

import MySQLdb as mysql


# 1.创建mysql数据库连接,获取连接对象
conn = mysql.connect(host='39.105.56.50', port=3306,
                     user='root', password='Yukl.911',
                     database='hrs', charset='utf8mb4',
                     connect_timeout=3)
try:
    # 2.基于连接对象创建游标对象
    with conn.cursor(mysql.cursors.DictCursor) as cursor:

        # 3.通过游标对象向数据库发出SQL语句,并获取执行结果
        cursor.execute(
            'select dno,dname,dloc from tb_dept'
        )
        # 4.通过游标获取数据
        # fetchone() fetchmany(n) fetchall()
        for row in cursor.fetchall():
            print(row)
finally:
    # 5.关闭连接,释放资源
    conn.close()
